package com.wyh.lotterysystem.controller;

import com.wyh.lotterysystem.common.pojo.CommonResult;
import com.wyh.lotterysystem.common.utils.JacksonUtil;
import com.wyh.lotterysystem.controller.request.CreatePrizeRequest;
import com.wyh.lotterysystem.controller.request.PrizeListRequest;
import com.wyh.lotterysystem.controller.response.PrizeListResponse;
import com.wyh.lotterysystem.service.PrizeService;
import com.wyh.lotterysystem.service.dto.PageDTO;
import com.wyh.lotterysystem.service.dto.PrizeListDTO;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestPart;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;

@RestController
@Slf4j
public class PrizeController {

    @Resource(name = "prizeServiceImpl")
    PrizeService prizeService;

    @RequestMapping("/prize/create")
    public CommonResult<Integer> createPrize(@RequestPart("request") @Validated CreatePrizeRequest request
            , @RequestPart("file") MultipartFile file){
        //打印日志
        log.info("创建奖品，prize：{}", JacksonUtil.writeValueAsString(request));

        //调用service层代码
        Integer prize = prizeService.createPrize(request, file);

        //返回
        return CommonResult.success(prize);
    }

    @RequestMapping("/prize/find-list")
    public CommonResult<PrizeListResponse> findPrizeList(PrizeListRequest request){
        //打印日志
        log.info("查找奖品列表，page：{}", request.getCurrentPage());

        //调用service层
        PageDTO<PrizeListDTO> prizeList = prizeService.findPrizeList(request);

        //构造返回值
        PrizeListResponse responses = new PrizeListResponse();
        responses.setRecords(prizeList.getRecord().stream().map(prizeListDTO -> {
            PrizeListResponse.PrizeList prizeList1 = new PrizeListResponse.PrizeList();
            BeanUtils.copyProperties(prizeListDTO,prizeList1);
            return prizeList1;
        }).toList());

        //返回
        return CommonResult.success(responses);
    }

}
